using System.Collections.Generic;
using System.Linq;
using System.Windows;
using StartupSolution.Models;

namespace StartupSolution.ViewModel.Common
{
    public class SamplesViewModel : ObservableModel
    {
        public SamplesViewModel()
        {
            _samples = new List<FrameworkElement>();
            _selecetedIndex = 0;

        }

        #region Properties
        public List<string> SamplesNames
        {
            get
            {
                return this.Samples.Select(sample => sample.Tag.ToString()).ToList();
            }
        }
        /// <summary>
        /// Represents a list of Samples
        /// </summary>
        protected List<FrameworkElement> Samples
        {
            get { return _samples; }
            set
            {
                _samples = value;
                this.OnPropertyChanged("Samples");
            }
        }
        private List<FrameworkElement> _samples;
        private int _selecetedIndex;
        /// <summary>
        /// Represents the currently selected index of sample in the Samples list
        /// </summary>
        public int SelectedSampleIndex
        {
            get { return _selecetedIndex; }
            set
            {
                if (value < 0 || value > this.Samples.Count) return;
                //if (value == _selecetedIndex) return;
                _selecetedIndex = value;
                this.ShowSample(value);
                this.OnPropertyChanged("SampleSelectedIndex");
            }
        }
        public FrameworkElement SelectedSample
        {
            get
            {
                if (this.Samples.Count == 0) return null;
                return this.Samples[SelectedSampleIndex];
            }
        }

        #endregion

        #region Methods

        public void AddSample(FrameworkElement sample)
        {
            _samples.Add(sample);
        }
        public void AddSample(FrameworkElement sample, string name)
        {
            sample.Tag = name;
            _samples.Add(sample);
        }
        /// <summary>
        /// Returns an index of the previous sample in Samples list
        /// </summary>
        /// <returns></returns>
        public int GetPreviousSampleIndex()
        {
            _selecetedIndex = _selecetedIndex == 0 ? this.Samples.Count - 1 : _selecetedIndex - 1;
            return _selecetedIndex;
        }
        /// <summary>
        /// Returns an index of the next sample in Samples list
        /// </summary>
        /// <returns></returns>
        public int GetNextSampleIndex()
        {
            _selecetedIndex = (_selecetedIndex + 1) % this.Samples.Count;
            return _selecetedIndex;
        }

        /// <summary>
        /// Shows a sample with the passed index
        /// </summary>
        /// <param name="sampleIndex"></param>
        public void ShowSample(int sampleIndex)
        {
            if (sampleIndex < 0 || sampleIndex > this.Samples.Count) return;

            this.HideAllSamples();
            this.Samples[sampleIndex].Visibility = Visibility.Visible;
        }

        /// <summary>
        /// Shows the next sample in the Samples list
        /// </summary>
        public void ShowNextSample()
        {
            this.HideAllSamples();
            this.SelectedSampleIndex = GetNextSampleIndex();
        }
        /// <summary>
        /// Shows the previous sample in the Samples list
        /// </summary>
        public void ShowPreviousSample()
        {
            this.HideAllSamples();
            this.SelectedSampleIndex = GetPreviousSampleIndex();
        }
        /// <summary>
        /// Hides all samples in the Samples list
        /// </summary>
        public void HideAllSamples()
        {
            foreach (FrameworkElement sample in this.Samples)
            {
                sample.Visibility = Visibility.Collapsed;
            }
        }
        #endregion

    }
}